//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package messages;

import common.ByteStream;
import common.RoadsideShop;
import common.Utils;
import common.decompressedData;

import java.util.LinkedHashMap;

public class VisitedHomeData extends Base {
    public static final int msgType = 24102;
    private Long visitedId = null;
    private Long homeId = null;
    private Long accountId = null;
    private static final int ASIZE = 256;
    private LinkedHashMap<Integer, Integer> homeInventory = new LinkedHashMap();
    private LinkedHashMap<Integer, Integer> visitedInventory = new LinkedHashMap();
    private RoadsideShop homeShop;
    private RoadsideShop visitedShop;
    private String homeData = new String();
    private String visitedData = new String();

    public VisitedHomeData() {
        super(24102);
    }

    public void encode(ByteStream stream) {
    }

    public void decode(ByteStream dstream) {
        byte[] vhdBytes = dstream.getCopyOfByteArray();
        this.isDecoded = false;
        if(this.homeId != null) {
            Integer homeStartIndex = this.identifierSerch(vhdBytes, this.accountId.longValue(), this.homeId.longValue());
            Integer visitedStartIndex = Integer.valueOf(0);
            if(homeStartIndex != null && visitedStartIndex != null) {
                ByteStream visitedstream = null;
                ByteStream homestream = null;
                if(visitedStartIndex.intValue() < homeStartIndex.intValue()) {
                    dstream.skip(visitedStartIndex.intValue());
                    visitedstream = new ByteStream(dstream.readBytes(homeStartIndex.intValue() - visitedStartIndex.intValue()));
                    homestream = new ByteStream(dstream.getUnreadBytes());
                }

                if(homeStartIndex.intValue() < visitedStartIndex.intValue()) {
                    dstream.skip(homeStartIndex.intValue());
                    homestream = new ByteStream(dstream.readBytes(visitedStartIndex.intValue() - homeStartIndex.intValue()));
                    visitedstream = new ByteStream(dstream.getUnreadBytes());
                }

                if(visitedstream != null && homestream != null) {
                    this.visitedId = Long.valueOf(visitedstream.readLong().longValue());
                    visitedstream.skip(8);
                    int itemslen = visitedstream.readShort();

                    int numslots;
                    int i;
                    for(numslots = 0; numslots < itemslen; ++numslots) {
                        int d = visitedstream.readInt();
                        i = visitedstream.readInt();
                        this.visitedInventory.put(Integer.valueOf(d), Integer.valueOf(i));
                    }

                    visitedstream.skip(274);
                    numslots = visitedstream.readInt();
                    if(numslots >= 0) {
                        this.visitedShop = new RoadsideShop(numslots);
                        this.visitedShop.decode(visitedstream);
                    }

                    decompressedData var13 = Utils.tryToDecompressData(visitedstream.getUnreadBytes());
                    if(var13.resultString.length() > 0) {
                        this.visitedData = var13.resultString;
                    }

                    homestream.skip(16);
                    itemslen = homestream.readShort();

                    for(i = 0; i < itemslen; ++i) {
                        int identifier = homestream.readInt();
                        int value = homestream.readInt();
                        this.homeInventory.put(Integer.valueOf(identifier), Integer.valueOf(value));
                    }

                    homestream.skip(274);
                    numslots = homestream.readInt();
                    if(numslots >= 0) {
                        this.homeShop = new RoadsideShop(numslots);
                        this.homeShop.decode(homestream);
                    }

                    var13 = Utils.tryToDecompressData(homestream.getUnreadBytes());
                    if(var13.resultString.length() > 0) {
                        this.homeData = var13.resultString;
                    }

                    this.isDecoded = true;
                }
            }
        }

    }

    private byte[] get2LongsAs16Bytes(long value, long value1) {
        byte[] dst = new byte[]{(byte)((int)(value >> 56)), (byte)((int)(value >> 48)), (byte)((int)(value >> 40)), (byte)((int)(value >> 32)), (byte)((int)(value >> 24)), (byte)((int)(value >> 16)), (byte)((int)(value >> 8)), (byte)((int)(value >> 0)), (byte)((int)(value1 >> 56)), (byte)((int)(value1 >> 48)), (byte)((int)(value1 >> 40)), (byte)((int)(value1 >> 32)), (byte)((int)(value1 >> 24)), (byte)((int)(value1 >> 16)), (byte)((int)(value1 >> 8)), (byte)((int)(value1 >> 0))};
        return dst;
    }

    private Integer identifierSerch(byte[] y, long accid, long hid) {
        boolean m = true;
        int n = y.length;
        int[] qs_bc = new int[256];
        byte[] x = this.get2LongsAs16Bytes(hid, accid);

        int i;
        for(i = 0; i < 256; ++i) {
            qs_bc[i] = 17;
        }

        for(i = 0; i < 16; ++i) {
            int idx = x[i] & 255;
            qs_bc[idx] = 16 - i;
        }

        for(i = 0; i <= n - 16; i += qs_bc[y[i + 16] & 255]) {
            if(this.bytesCompare(y, i, x)) {
                return Integer.valueOf(i);
            }
        }

        return null;
    }

    private boolean bytesCompare(byte[] src, int srcIdx, byte[] pattern) {
        for(int i = 0; i < pattern.length; ++i) {
            if(src[srcIdx + i] != pattern[i]) {
                return false;
            }
        }

        return true;
    }

    public Long getVisitedId() {
        return this.visitedId;
    }

    public void setHomeId(long ownid) {
        this.homeId = Long.valueOf(ownid);
    }

    public void setAccountId(long aid) {
        this.accountId = Long.valueOf(aid);
    }

    public Long getHomeId() {
        return this.homeId;
    }

    public RoadsideShop getHomeShop() {
        return this.homeShop;
    }

    public RoadsideShop getVisitedShop() {
        return this.visitedShop;
    }

    public LinkedHashMap<Integer, Integer> getHomeInventory() {
        return this.homeInventory;
    }

    public LinkedHashMap<Integer, Integer> getVisitedInventory() {
        return this.visitedInventory;
    }

    public int getHomeLevel() {
        return this.getLevel(this.homeInventory);
    }

    public int getVisitedLevel() {
        return this.getLevel(this.visitedInventory);
    }

    private int getLevel(LinkedHashMap<Integer, Integer> inventory) {
        if(inventory != null) {
            Integer l = (Integer)inventory.get(Integer.valueOf(1900002));
            if(l != null) {
                return l.intValue();
            }
        }

        return 0;
    }

    public String getHomeData() {
        return this.homeData;
    }

    public String getVisitedData() {
        return this.visitedData;
    }
}
